import http.client,unittest,HTMLTestRunner,pymysql,json,re

class http_test(unittest.TestCase):
    # def test_get(self):
    #     #建立连接
    #     con=http.client.HTTPConnection(host='localhost',port=80)
    #     #发送GET请求
    #     con.request(method='GET',url='/agileone/')
    #     #获取响应
    #     res=con.getresponse().read().decode()
    #     print(res)
    #     #响应断言
    #     self.assertIn('AgileOne - Welcome to Login',res)
    #     con.close()



    def test_post(self):
        # 建立连接
        con = http.client.HTTPConnection(host='localhost', port=80)
        #发送POST请求
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type':' application/x-www-form-urlencoded'})
        #三个获取必须分别连接

        #获取响应---返回successful
        res =con.getresponse().read().decode()
        #print("001"+res)
        con.close()
        #获取响应头信息
        con = http.client.HTTPConnection(host='localhost', port=80)
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type':' application/x-www-form-urlencoded'})
        headers=con.getresponse().getheaders()
        #print(headers)
        # 依据响应头名称--获取cookie
        con = http.client.HTTPConnection(host='localhost', port=80)
        con.request(method='POST',url='/agileone/index.php/common/login',
                    body='username=admin&password=admin&savelogin=true',
                    headers={'Content-Type':' application/x-www-form-urlencoded'})
        header=con.getresponse().getheader('Set-Cookie')
        print("1",header)
        #响应断言
        self.assertEqual(res,'successful')
        con.close()
    # def test_add(self):
    #     #登录：
    #     con = http.client.HTTPConnection('localhost', 80)
    #     con.request(method='POST', url='/agileone/index.php/common/login',
    #                 body='username=admin&password=admin&savelogin=true',
    #                 headers={'Content-Type': ' application/x-www-form-urlencoded'})
    #     cookie=con.getresponse().getheader('Set-Cookie')
    #     print("2",cookie)
    #     print(cookie.split(';')[0])
    #     cookie_1=cookie.split(';')[0]
    #     #新增公告
    #     con = http.client.HTTPConnection(host="localhost",port=80)
    #     con.request("POST","/agileone/index.php/notice/add",
    #                 "headline=7788&content=<br />&scope=1&expireddate=2019-02-06",
    #                 {"Content-Type":"application/x-www-form-urlencoded","Cookie":cookie_1}
    #                 )
    #     res=con.getresponse().read().decode()
    #     print(res)
    #     self.assertGreater(int(res),1)









if __name__ == '__main__':
    http_test().test_post()
    # unittest.main()
    # suite=unittest.TestSuite()
    # suite.addTests(unittest.TestLoader().loadTestsFromTestCase(http_test))
    # with open('http_测试报告.html','w',encoding='utf-8') as file:
    #     HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,
    #                                   title='测试报告',description='HTTP测试报告').run(suite)
